/*
 * Created on Feb 12, 2008
 */
package maxygen;

import com.cycling74.max.Atom;
import com.cycling74.max.MaxObject;


/**
 * Utility class for parsing data from Max/MSP binbufs.
 * This is compatible with Max/MSP 4.6.3. For Max 5 BinBufUtility5 should be used
 * 
 * @author  martinrobinson
 */
public class PatcherFileUtility4 implements PatcherFileUtilityBase {
    
    
    public String commentFromPatcher(libmaxygen thisob, String obname, boolean convertHtml,
            String captions[], String patchCaption, String editPatchCaption, String anchorPrefix)
    {
        String binbuf = thisob.getpsave(thisob, obname);
        
        return commentFromBinbuf(binbuf, convertHtml, 
                                 captions, patchCaption, 
                                 editPatchCaption, anchorPrefix);
    }
    
    /**
     * Extract the comment object text from a comment object's binbuf.
     * 
     * @param binbuf        the text obtained from the comment object's "psave" method
     * @param convertHtml   convert the naive text formatting to HTML
     * @param captions      array of caption names/titles for figures
     * @param anchorPrefix  the prefix to be used for HTML anchors in this file
     * @return              the extracted text
     */
    public  String commentFromBinbuf(String binbuf, boolean convertHtml, 
            String captions[], String patchCaption, String editPatchCaption, String anchorPrefix)
    {
        if(binbuf == null) return "";
            
        binbuf = StringUtility.tidyBinbuf(binbuf);

        Atom list[] = Atom.parse(binbuf);
        int i; 
        
        i = 0;
        boolean foundComment = false;
        while(i < list.length) {
            if(list[i].isString() && list[i].getString().equals("comment")) {
                foundComment = true;
                break;
            }
            
            i++;
        }
        
        if(!foundComment) return null;
        int start = i + 5;
        int end = list.length-1;
        
        Atom newList[] = new Atom[end-start+1];
        
        int j = start;
        for(i = 0; i < newList.length; i++, j++) {
            newList[i] = list[j];
        }
        
        return StringUtility.atomBufToString(newList, convertHtml, 
                captions, patchCaption, editPatchCaption, anchorPrefix);
    }
    
    
    
    public IOLet ioletFromPatcher(libmaxygen thisob, String obname, IOLet.Type mode, int index, boolean convertHtml, 
            String captions[], String patchCaption, String editPatchCaption, String anchorPrefix)
    {
        String binbuf = thisob.getpsave(thisob, obname);
        
        return ioletFromBinbuf(binbuf, mode, index, convertHtml, 
                //regionCaptions, patchCaption, editPatchCaption, anchorPrefix);
                captions, patchCaption, editPatchCaption, anchorPrefix);
    }
    
    /**
     * Extract the inlet/outlet assistance text and index info from an in/in~/out/out~ object's binbuf.
     * 
     * @param binbuf        the text obtained from the in/in~/out/out~ object's "psave" method
     * @param mode          which type of IOLet
     * @param index         inlet/outlet index
     * @param convertHtml   convert the naive text formatting to HTML
     * @param captions      array of caption names/titles for figures
     * @param anchorPrefix  the prefix to be used for HTML anchors in this file
     * @return              the extracted IOLet contain
     */
    public  IOLet ioletFromBinbuf(String binbuf, IOLet.Type mode, int index, boolean convertHtml, 
            String captions[], String patchCaption, String editPatchCaption, String anchorPrefix)
    {
        if(binbuf == null) return null;
        
        binbuf = StringUtility.tidyBinbuf(binbuf);
        
        Atom list[] = Atom.parse(binbuf);
        int i;
        int coords[] = IOLet.getIOLetCoords(list, mode);
        index = IOLet.getIOLetIndex(list, mode, index);
        
        i = 0;
        boolean foundComment = false;
        String comtag = IOLet.getModeComtag(mode);
        while(i < list.length) {
            if(list[i].isString() && list[i].getString().equals(comtag)) {
                foundComment = true;
                break;
            }
            
            i++;
        }
        
        if(!foundComment)
            return new IOLet(mode, index, "", coords[0], coords[1]);
            
        int start = i + 1;
        
        i = start;
        while(i < list.length) {      
            if(list[i].isString() && list[i].getString().equals("#P")) {
                break;
            }
            
            i++;
        }
        int end = i;
        
        Atom newList[] = new Atom[end-start];
        
        int j = start;
        for(i = 0; i < newList.length; i++, j++) {
            newList[i] = list[j];
        }
        
        return new IOLet(mode, index, StringUtility.atomBufToString(newList, convertHtml, 
                captions, patchCaption, editPatchCaption, anchorPrefix), coords[0], coords[1]);
    }
    
    public String panelFromPatcher(libmaxygen thisob, String obname, boolean convertHtml, 
            String captions[], String patchCaption, String editPatchCaption, String anchorPrefix)
    {
        String binbuf = thisob.getpsave(thisob, obname);
        return panelFromBinbuf(binbuf, convertHtml, 
                captions, patchCaption, editPatchCaption, anchorPrefix);
    }
    
    /**
     * Extract the dimensions from a panel object's binbuf.
     * 
     * @param binbuf        the text obtained from the panel object's "psave" method
     * @param convertHtml   convert the naive text formatting to HTML (redundant in this context)
     * @param captions      array of caption names/titles for figures
     * @param anchorPrefix  the prefix to be used for HTML anchors in this file
     * @return              the panel data as text
     */
    public  String panelFromBinbuf(String binbuf, boolean convertHtml, 
            String captions[], String patchCaption, String editPatchCaption, String anchorPrefix)
    {
        if(binbuf == null) return "";
        
        binbuf = StringUtility.tidyBinbuf(binbuf);
        
        Atom list[] = Atom.parse(binbuf);
        int i; 
        
        i = 0;
        boolean foundPanel = false;
        while(i < list.length) {
            if(list[i].isString() && list[i].getString().equals("panel")) {
                foundPanel = true;
                break;
            }
            
            i++;
        }
        
        if(!foundPanel) return null;
        int start = i + 1;
        
        i = start;
        while(i < list.length) {      
            if(list[i].isString() && list[i].getString().equals("#X")) {
                break;
            }
            
            i++;
        }
        int end = i;
        
        Atom newList[] = new Atom[end-start];
        
        int j = start;
        for(i = 0; i < newList.length; i++, j++) {
            newList[i] = list[j];
        }
        
        return StringUtility.atomBufToString(newList, convertHtml, 
                captions, patchCaption, editPatchCaption, anchorPrefix);
    }
}
